//
//  LLBaseViewController.swift
//  新浪微博
//
//  Created by mac on 16/5/22.
//  Copyright © 2016年 mac.com. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol LLVisitorLoginViewDelegate:NSObjectProtocol{
optional func userWillLogin()
    func userWillRegisiter()


}
class LLVisitorLoginView: UIView {
    weak var visitorDelegate:LLVisitorLoginViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func updateUI(title: String, imageName: String,isHome: Bool = false){
        if isHome{
        startAnimation()
        }else{
        largeHouse.hidden = true
        bringSubviewToFront(circleIcon)
        }
    circleIcon.image = UIImage(named: imageName)
        tipLabel.text = title
//        print(title)
    }
    private func setupUI(){
        addSubview(circleIcon)
        addSubview(backIcon)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        circleIcon.snp_makeConstraints { (make) in
            //make 就是circleIcon 取消了frame布局的对象
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        largeHouse.snp_makeConstraints { (make) in
            //            make.centerY.equalTo(circleIcon.snp_centerY)
            //            make.centerX.equalTo(circleIcon.snp_centerX)
            make.center.equalTo(circleIcon.snp_center)
        }
        
        //提示文案
        tipLabel.snp_makeConstraints { (make) in
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
            //约束宽度
            //            make.width.equalTo(230)
            //约束高度 解决页面跳动的问题
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            
        }
        
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        backIcon.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        //一般使用sRGB取值
        let value: CGFloat = 237.0 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        loginBtn .addTarget(self, action: Selector("loginBtnDidClick"), forControlEvents: .TouchUpInside)
        registerBtn .addTarget(self, action: Selector("regisiterBtnDidClick"), forControlEvents: .TouchUpInside)

        

    
    }
    @objc  private func loginBtnDidClick(){
    visitorDelegate?.userWillLogin?()
    }
    @objc private func regisiterBtnDidClick(){
    visitorDelegate?.userWillRegisiter()
    }
    //给圆圈添加动画
    private func startAnimation(){
    let basic = CABasicAnimation(keyPath: "transform.rotation")
        basic.duration = 15
        basic.repeatCount = MAXFLOAT
        basic.toValue = 2 * M_PI
        basic.removedOnCompletion = false
        circleIcon.layer.addAnimation(basic, forKey: nil)
    
    }
    
    //懒加载子视图
    //遮罩
 private lazy var backIcon = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //圈圈
    private lazy var circleIcon : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示登录注册的文案
    private lazy var tipLabel: UILabel = {
    let l = UILabel()
        l.text = "hahhaha"
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        l.textAlignment = .Center
        l.numberOfLines = 0
        l.preferredMaxLayoutWidth = 230
    l.sizeToFit()
        return l
    
    }()
    //登录按钮
    private lazy var loginBtn: UIButton={
    let login = UIButton()
        login.setTitle("登录", forState: .Normal)
        login.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        login.titleLabel?.font = UIFont.systemFontOfSize(14)
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        login.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        return login
    }()
    
    //注册
    private lazy var registerBtn: UIButton={
        let register = UIButton()
        register.setTitle("注册", forState: .Normal)
        register.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        register.titleLabel?.font = UIFont.systemFontOfSize(14)
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        register.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        return register
    }()

    
    

}
